﻿<?php
session_start();
date_default_timezone_set("Asia/Chongqing");
include("../config/connect.php");
require_once("../public/common.php");
require_once("../public/biaoqing.php");

if( !HasLogin() ) 
{
	$url = "Location:login.php?ref=edit_items.php";
	header($url);
}

$email = $_SESSION["email"];
$userid = $_SESSION["userid"];
$nickname = $_SESSION["nickname"];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?PHP echo $nickname; ?>家的宝贝 - 兔丝街</title>
<link type="text/css" rel="stylesheet" href="../css/base.css" />
<link type="text/css" rel="stylesheet" href="../css/person.css" />
</head>
<body class="bShare">
<div id="wrapper">
<!---head kaishi--->
     <?php
		include("../headtop.php"); 
	?>
<!-----head 结束----->    
    <div id="main" class="person">
    	<!---左侧开始--->
    	<div class="grid_3">
        	<div class="person_left">
            	<div class="person_info">
                    <!---侧栏导航 kaishi--->
					 <?php
                         include("sidebarnav.php"); 
                    ?>
                    <!------------>
                </div>
            </div>
        </div>
 
<?php
$id = CheckUrlParamNoDie("id");
$opt = CheckUrlParamNoDie("opt");
$itemid = "";
$itemname = "";
$systag = "";
$tag = "";
$fee = 0;
$commission = 0;
$url = "";
$picurl = "";
$desc = "";

if( $opt == "save" && isset( $_POST["submit"] ))
{
	$itemid = GetPostParamStr("itemid");
	$itemname = mysql_real_escape_string(GetPostParamStr("itemname"));
	$systag = mysql_real_escape_string(trim(GetPostParamStr("systag")));
	$tag = mysql_real_escape_string(" ".trim(GetPostParamStr("tag"))." ");
	$fee = GetPostParamStr("fee");
	$commission = GetPostParamStr("commission");
	$url = mysql_real_escape_string(GetPostParamStr("url"));
	$picurl = mysql_real_escape_string(GetPostParamStr("picurl"));
	$desc = mysql_real_escape_string(GetPostParamStr("desc"));

	$sql = "insert into goods_items (num_iid, item_title, fee, commission, pic_url, url, user_id, sys_tag, tag, r_desc, state) values ('$itemid', '$itemname', $fee, $commission, '$picurl', '$url', $userid, '$systag', '$tag', '$desc', 0)";

	mysql_query( $sql );
	if (mysql_affected_rows() == 0)
	{
		if ($id == "")
			echo "<script>alert('插入失败，请找程序员。');</script>";
		else
			echo "<script>alert('没有条目被更新，如有疑问，请找程序员。');</script>";
	}
	else
	{
		if ($id == "")
			echo "<script>alert('添加成功!');window.history.go(-2);</script>";
		else
			echo "<script>alert('编辑成功!');window.history.go(-2);</script>";
	}
}


?>  
        <!---右侧--->
      	<div class="grid_9"> 
        	<div id="editGoods">
                <div class="edit_left">
                	<div class="Per">
                        <ul>
                            <li><a href="index.php">我的宝贝</a></li>
                            <li><a href="like.php">我喜欢的宝贝</a></li>

                        </ul>
                    </div>
                    <form id="frm" action="?id=<?PHP echo $id; ?>&opt=save" method="post" onsubmit="javascript:return OnSubmit()">
                        <div class="edit_div">
                            <div class="s1">请输入宝贝的网址：</div>
                            <div class="s2"><input type="text" size="70" name="itemurl" id="itemurl" height="20" value="<?PHP echo $url; ?>" /></div>
                            <div class="s3"><input type="button" onclick="OnSync()" value="确定" class="ed_h" /></div>
                        </div>
                        <div style="padding-left:90px; margin-bottom:10px;"><div class="s2" id="ishasitem"></div></div>
                        <!----点击确定显示----->
                        <div id="DisplayProduct" style="display:none;">
                            <div class="edit_div">
                                <div class="s1">名称：</div>
                                <div class="s2"><input type="text" size="70" name="itemname" id="itemname" height="20" value="<?PHP echo $itemname; ?>" /></div>
                            </div>
                            <div class="edit_div">
                                <div class="s1">标签：</div>
                                <div class="s2"><input type="text" size="70" height="20" name="tag" id="tag" value="" /></div>
                            </div>
                            <div class="edit_div">
                                <div class="s1">推荐标签：</div>
                                <div class="s2"><div id="recomtag"><?PHP echo $tag; ?></div></div>
                            </div>
                            <div class="edit_div">
                                <div class="s1">推荐语：</div>
                                <div class="s2"><textarea name="desc" id="desc" cols="56" rows="7"><?PHP echo $desc; ?></textarea></div>
                                <div class="s3"><?PHP echo ShowBiaoQing("OnBiaoQing", 20); ?></div>
                            </div>
                            <div class="edit_div">
                                <input type="hidden" size="70" name="url" id="url" height="20" value="<?PHP echo $url; ?>" />
                                <input type="hidden" name="itemid" id="itemid" value="<?PHP echo $itemid; ?>" />
                                <input type="hidden" name="fee" id="fee" value="<?PHP echo $fee; ?>" />
                                <input type="hidden" name="commission" id="commission" value="<?PHP echo $commission; ?>" />
                                <input type="hidden" name="taghidden" id="taghidden" value="<?PHP echo $tag; ?>" />
                                <input type="hidden" name="picurl" id="picurl" value="<?PHP echo $picurl; ?>" />
                                <div class="btn"><input type="submit" name="submit" value="保存" class="ed_h" style=" width:100px; margin:0 auto;" /></div>
                            </div>
                    	</div>
                    </form>
                </div>
        
       			<div class="edit_right"><div class="edit_img" id="editImg" style="display:none;"><img id="pic" <?PHP if ($picurl!="") echo "src='".$picurl."'"; ?> /></div></div>
            </div>
            <!----右侧结束-------->
            
        </div>
    </div>
</div>
<!---网尾 kaishi--->
 <?php
     include("../networkend.php"); 
?>
<!------------>
</body>
</html>

<script language="javascript">
function gup(id) {
	var dz_url = document.getElementById("itemurl").value;
    id = id.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+id+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(dz_url);
    if( results == null ) {
        return "";
    } else {
        return results[1];
    }
}
function OnSync()
{
	var url_id = "";
	if(gup("id") != ""){
		url_id = gup("id");  //个人 商城
	}
	else if(gup("item_num_id") != "" ){
		url_id = gup("item_num_id");  //部分话旗舰店
	}	
	else if(gup("item_id") != "" ){
		url_id = gup("item_id");  //// 旅游、酒店
	}
	else if(gup("mallstItemId") != "" ){
		url_id = gup("mallstItemId");  //// 淘宝商城官方旗舰店
	}
	
	if (url_id == "")
		return;
	
//	alert(url_id);
	document.getElementById("itemid").value = url_id;

	var url = "http://www.tusijie.com/admin/find_item.php?itemid=" + url_id;
	//alert(url);
	$.ajax({	url: url,
			async: false,
			success:function(res)
			{
			//alert(res);
			var resObj = eval('(' + res + ')');
			//alert(resObj.id);
			if (resObj.code == 1)
			{
				var res_id = resObj.id; 
				document.getElementById("ishasitem").innerHTML="<font color='#FF0000'>已经有相同的宝贝哦</font>&nbsp;&nbsp;<a href='http://www.tusijie.com/item/?id="+ resObj.id + "' target='_blank'>点击查看宝贝</a>";
				DisPro.style.display="none";
			}
			else if (resObj.code == 0)
				document.getElementById("ishasitem").innerHTML="";
			else
				alert("判断是否存在宝贝出错！请找冰峰");
			}
		});
	
	//	
	var dz_url = document.getElementById("itemurl").value;
	var DisPro = document.getElementById("DisplayProduct");
	if(dz_url == "")
	{
		document.getElementById("ishasitem").innerHTML="<font color='#FF0000'>要输入您要推荐宝贝的地址哦</font>";
		return false;
	}
	else
	{
		DisPro.style.display="block";
		document.getElementById("editImg").style.display="block";		
			
		url = "http://www.tusijie.com/admin/get_item_info.php?itemid=" + url_id;
		$.ajax({	url: url,
				async: false,
				success:function(res)
				{
					//alert(res);
					var resObj = eval('(' + res + ')');
					if (resObj.code == 1)
					{
						document.getElementById("url").value=resObj.click_url ? resObj.click_url : resObj.detail_url;
						document.getElementById("itemname").value=resObj.title;
						document.getElementById("taghidden").value=resObj.tag;
						var str=resObj.tag; 
						var result=str.split(' ');
						var arr = new Array();
						var recomtag = "";
						for(var i=0;i<result.length-1;i++)
							recomtag += '<span onclick="OnClickTag(this)">' + result[i] + '</span>';
							
						document.getElementById("recomtag").innerHTML=recomtag;
						document.getElementById("picurl").value=resObj.pic_url;
						document.getElementById("fee").value=resObj.price;
						document.getElementById("commission").value=resObj.commission ? resObj.commission : 0;
						document.getElementById("pic").src=resObj.pic_url ? resObj.pic_url : "";
					}
				}
			});
	}
}
function OnClickTag(obj)
{
	document.getElementById("tag").value = document.getElementById("tag").value + " " + obj.innerHTML;
}
//itemurl  itemname  tag desc  document.myform.content.value
function OnSubmit()
{
	if (document.getElementById("itemurl").value=="")
	{
		alert("宝贝URL不能为空！");
		return false;
	}
	else if (document.getElementById("itemname").value=="")
	{
		alert("宝贝名称不能为空！");
		return false;
	}
	else if (document.getElementById("tag").value=="")
	{
		document.getElementById("tag").value = document.getElementById("taghidden").value;
		//return false;
	}
	
	return true;
//	document.getElementById("frm").submit();
}

function OnBiaoQing(str)
{
	var obj = document.getElementById("desc");
    var ubbLength=obj.value.length; 
    obj.focus(); 
    if(typeof document.selection !="undefined") 
		document.selection.createRange().text=str; 
    else 
		obj.value=obj.value.substr(0,obj.selectionStart)+str+obj.value.substring(obj.selectionStart,ubbLength); 
}
</script>

<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F22b7662ec7d02c816ddfd8f6df6e5cea' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-20984072-3']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>